<?php

namespace App\Http\Controllers\Home;

use App\Models\User;
use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Mail;


//登录控制器
class LoginController extends Controller
{
    //登录页
	public function index(){

	    //dd($_SERVER);

      // $a = explode('/',$_SERVER['REDIRECT_URL']);
       //dd($a);
        //获取上一个页面
        $referer = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'/';
        if(strpos($referer,'savePass') !==false){//有  (判断是否重重置密码页面过来)

            session(['prevPage' => '/']);

        }else{
            session(['prevPage' => $referer]);
        }



        //登录页面
        return view("home.login");

	}

	//处理登录
    public function check(Request $request){

	   // dd($request->all());
        //接收数据

        $email = $request->input("email");
        $pass = $request->input("pass");

        //取数据库中进行查询
        $data  = User::where("email",$email)->first();
        if($data){
            if($data->status==1){
                //判断密码是否正确
                if($pass == Crypt::decrypt($data->pass)){

                    session(['lenovoHomeUserInfo.email'=>$data->email]);
                    session(['lenovoHomeUserInfo.name'=>$data->name]);
                    session(['lenovoHomeUserInfo.id'=>$data->id]);

                    return redirect(session('prevPage'));
                }else{
                    return back()->with("error","邮箱密码不一致");
                }
            }else{
                return back()->with("error","账号未激活");
            }


        }else{
            return back();
        }

    }

    //退出的方法
    public function logout(Request $request){
        //$request->session()->flush();
        $request->session()->forget('lenovoHomeUserInfo.email');
        $request->session()->forget('lenovoHomeUserInfo.name');
        $request->session()->forget('lenovoHomeUserInfo.id');
        return redirect('/');

    }

    //找回密码
    public function zhaohui(Request $request){
        //$_POST
        if($request->isMethod('post')){
            //接收数据
            $email=$request->input('email');

            //获取数据
            $data  = User::where("email",$email)->first();

            //判断数据是否正确
            if($data){
                //我们使用的是一个闭包函数
                Mail::send('mail.zh',["id"=>$data->id,"token"=>$data->token],function($message) use ($email){
                    $message->to($email);
                    $message->subject("找回密码");
                });

                //加载找回密码提示页面
                $mailArr=explode("@",$email);
                $href="mail.".$mailArr['1'];

                return view("home.zhaohuiTishi")->with('href',$href);

            }else{
                return back();
            }
        }


        return view("home.zhaohui");
    }

    //修改密码
    public function savePass(Request $request,$id,$token){

       // var_dump($_POST);
        //判断post 是否存在 存在修改密码 不存在修改密码页面
        if($request->isMethod('post')){
            //判断密码是否一致
            if($request->input('pass')==$request->input('repass')){

                //密码长度
                if(strlen($request->input('pass'))>=6 && strlen($request->input('pass'))<=12){

                    //格式化数据并且进行修改
                    $data = array();
                    $data['token']=str_random(50);
                    $data['pass']=Crypt::encrypt($request->input('pass'));
                    if(User::where("id",$id)->update($data)){



                        return redirect("/login");
                    }else{
                        return back()->with('error','修改失败');
                    }

                }else{
                    return back()->with('error','密码长度不对');
                }

            }else{
                return back()->with('error','两次输入密码不一致');
            }
        }

        return view("home.savePass");
    }
	
}
